/**
 * @Author: 陈瑞鹏
 * @Date:   2022-10-18 14:59:46
 * @Last Modified by:   陈瑞鹏
 * @Last Modified time: 2022-11-10 18:11:40
 */
import abiao_utils from 'abiao_utils';
import express from 'express';
import { userApi } from '../config/api.config';
import reward from '../controllers/reward.controller';
import user from '../controllers/user.contorller';
import { Code } from '../enum/code.enum';
import { FUNC_NAME } from '../enum/itemLogs.enum';
import Equip from '../models/equip/equip.model';
import Hero from '../models/hero/hero.model';
import User from '../models/user/user.model';
import Redis from '../utils/redis';
import Result from '../utils/result';
import utils from '../utils/utils';
const userRoute = express.Router();

/**
* @api {post} /user/saveLineup 保存阵容
* @apiGroup 用户
* @apiParam {Number} roleId 角色ID
* @apiParam {Array} warLineupArr 武将阵容数组
* @apiParam {Number} warTowerId 防御塔id
* @apiName upgrade
* @apiVersion 1.0.0
*/
userRoute.post(userApi.saveLineup, async (req, res) => {

  let { roleId, warLineupArr, warTowerId } = req.body;

  await User.updateOne({ roleId }, { warLineupArr, warTowerId });

  return res.json({ code: Code.Succ, message: '保存成功!' });
})

/**
* @api {post} /user/passLevel 过关
* @apiGroup 用户
* @apiParam {Number} roleId 角色ID
* @apiParam {Number} bigLv 大关卡
* @apiParam {Number} smallLv 小关卡
* @apiName passLevel
* @apiVersion 1.0.0
*/
userRoute.post(userApi.passGate, async (req, res) => {
  let { roleId, gate } = req.body;

  let waveInfo: any = await utils.get_id_json('gate/waveInfo.json', gate, 'gate');

  let desc = `第${gate}关`;

  await Redis.set_user_online_status(roleId);

  let goldCoin = Math.round(waveInfo['reward'])

  await reward.sendGoldCoin(roleId, goldCoin, `${desc} 基础数量${waveInfo['reward']}}`, FUNC_NAME.PASS_LEVEL);

  await User.updateOne({ roleId }, { gate, updateTime: abiao_utils.date() });

  let userInfo: any = await user.getUserInfo(roleId);
  let retData = {
    userInfo
  }

  return res.json({ code: Code.Succ, message: "过关成功!", data: retData });
})

/**
* @api {post} /user/luckDraw 幸运抽奖
* @apiGroup 用户
* @apiParam {Number} roleId 角色ID
* @apiParam {Number} type 1=单次, 2=十次
* @apiName luckDraw
* @apiVersion 1.0.0
*/
userRoute.post(userApi.luckDraw, async (req, res) => {
  let { roleId, type, isGuide } = req.body;

  let userInfo: any = await user.getUserInfo(roleId);
  let equipTzData: any = await utils.get_all_json('equip/equipment.json');

  let luckDrawData = [
    { type: reward.propName.goldCoin, name: '10万金币', num: 100000, chance: 0.1, t: 0 },
    { type: reward.propName.diamond, name: '50钻石', num: 50, chance: 0.1, t: 1 },
    { type: reward.propName.goldCoin, name: '50万金币', num: 500000, chance: 0.1, t: 2 },
    { type: reward.propName.equip, name: '随机装备', num: 1, id: abiao_utils.getRandomArrayElements(equipTzData, 1)[0].id, chance: 0.1, t: 3 },
    { type: reward.propName.goldCoin, name: '20万金币', num: 200000, chance: 0.1, t: 4 },
    { type: reward.propName.hero, name: '随机武将', num: 1, id: abiao_utils.getRandom(2001, 2028), chance: 0.1, t: 5 },
    { type: reward.propName.goldCoin, name: '10万金币', num: 100000, chance: 0.1, t: 6 },
    { type: reward.propName.diamond, name: '50钻石', num: 50, chance: 0.1, t: 7 },
    { type: reward.propName.goldCoin, name: '50万金币', num: 500000, chance: 0.1, t: 8 },
    { type: reward.propName.equip, name: '随机装备', num: 1, id: abiao_utils.getRandomArrayElements(equipTzData, 1)[0].id, chance: 0.1, t: 9 },
    { type: reward.propName.goldCoin, name: '20万金币', num: 200000, chance: 0.1, t: 10 },
    { type: reward.propName.hero, name: '随机武将', num: 1, id: abiao_utils.getRandom(2001, 2028), chance: 0.1, t: 11 },
  ];

  let desc = '';
  let ret: any = null;
  let luckDrawReward: any
  let luckDrawNum: number = userInfo.luckDrawNum
  switch (Number(type)) {
    case 1:
      desc = '单次抽奖';
      ret = await reward.reduceDiamond(roleId, 200, desc, FUNC_NAME.LUCK_DRAW);
      if (ret == -1) throw new Result(Code.Fail, '钻石不足');
      luckDrawReward = [await utils.chance(luckDrawData, 'chance', roleId)];
      // console.log(luckDrawReward);
      await reward.sendItem(roleId, desc, luckDrawReward, FUNC_NAME.LUCK_DRAW);
      break;
    case 2:
      desc = '十次抽奖';
      ret = await reward.reduceDiamond(roleId, 1800, desc, FUNC_NAME.LUCK_DRAW);
      if (ret == -1) throw new Result(Code.Fail, '钻石不足');
      luckDrawReward = [];
      for (let i = 0; i < 10; i++) {
        luckDrawData = [
          { type: reward.propName.goldCoin, name: '10万金币', num: 100000, chance: 0.1, t: 2 },
          { type: reward.propName.diamond, name: '50钻石', num: 50, chance: 0.1, t: 3 },
          { type: reward.propName.goldCoin, name: '50万金币', num: 500000, chance: 0.1, t: 1 },
          { type: reward.propName.equip, name: '随机装备', num: 1, id: abiao_utils.getRandomArrayElements(equipTzData, 1)[0].id, chance: 0.1, t: 6 },
          { type: reward.propName.goldCoin, name: '20万金币', num: 200000, chance: 0.1, t: 9 },
          { type: reward.propName.hero, name: '随机武将', num: 1, id: abiao_utils.getRandom(2001, 2028), chance: 0.1, t: 7 },
        ];
        let rewardReward = await utils.chance(luckDrawData, 'chance', roleId);
        luckDrawReward.push(rewardReward);
      }
      await reward.sendItem(roleId, desc, luckDrawReward, FUNC_NAME.LUCK_DRAW);
      luckDrawNum += 10;
      break;
  }
  await User.updateOne({ roleId }, { luckDrawNum })

  userInfo = await user.getUserInfo(roleId);
  let EquipRes: any = await Equip.find({ roleId });
  let HeroRes: any = await Hero.find({ roleId });
  return res.json({
    code: Code.Succ, message: "抽奖成功", data: {
      luckDrawReward,
      userInfo,
      EquipRes,
      HeroRes,
    }
  })
})

export default userRoute;
